<template>
  <div class="loginBox">
    <div class="loginModel">
      <div class="logoBox">
        <img src="../assets/img/logo.png" alt />
      </div>
      <div class="formBox">
        <el-form ref="loginForm" :model="loginForm" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名">
              <i slot="prefix" class="iconfont icon-user"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入密码" show-password>
              <i slot="prefix" class="iconfont icon-3702mima"></i>
            </el-input>
          </el-form-item>
          <el-row>
            <el-col :offset="15">
              <el-button type="primary" @click=" login() ">登录</el-button>
              <el-button type="info" @click="$refs.loginForm.resetFields()">重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data: () => {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login () {
      // 表单验证
      // 找到当前表单的dom对象
      // console.log(this.$refs.loginForm)
      this.$refs.loginForm.validate(async valid => {
        // console.log(valid)
        if (valid) {
          // 校验成功了发送axios请求
          // 结构data 并且冲命名
          var { data: dt } = await this.$http.post('login', this.loginForm)
          console.log(dt)
          if (dt.meta.status === 200) {
            // 存一个token
            window.sessionStorage.setItem('token', dt.data.token)
            // 跳转到首页
            this.$router.push('/home')
          } else {
            this.$message.error(dt.meta.msg)
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.loginBox {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .loginModel {
    width: 450px;
    height: 304px;
    background-color: #fff;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    position: relative;
    .logoBox {
      width: 148px;
      height: 148px;
      background-color: #fff;
      padding: 9px;
      box-sizing: border-box;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #eee;
      box-shadow: 0 0 10px #eee;

      img {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .formBox {
      position: absolute;
      bottom: 0;
      width: 100%;
      box-sizing: border-box;
      padding: 20px;
    }
  }
}
</style>
